﻿using System;
using WeChat.AspNet;
using WeChat.AspNet.Events;

namespace TodayYongxing
{
    /// <summary>
    /// 响应事件的处理对象，需要实现IResposeObject。
    /// </summary>
    public class ResposeObject : IResposeObject
    {
        /// <summary>
        /// 用户关注事件,当用户在关注/取消服务号或订阅号时的响应处理。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnSubscribe(object sender, SubscribeEventArgs e)
        {
            if (e.Type == SubscribeType.Subscribe)
            {
                try
                {
                    // 当用户关注今日永兴时，以多图文的方式进行响应，多图文的内容在Welcome类里面定义。
                    var news = new NewsMsg(e.ToUserName, e.FromUserName)
                        {
                            Articles = Welcome.Articles
                        };
                    var xml = news.ToString();
                    Logger.Log(string.Format("\r\nResponse:\r\n{0}", xml));
                    WeChatResponse.Write(xml);
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                }
                
            }
            // 取消关注时暂时未作处理。
        }

        /// <summary>
        /// 用户扫描事件处理，微信中的扫一扫。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnScan(object sender, ScanEventArgs e)
        {
            
        }

        /// <summary>
        /// 上报地理位置响应事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnReportLocation(object sender, ReportLocationEventArgs e)
        {
            
        }

        /// <summary>
        /// 自定义菜单点击时的响应事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnMenuClicked(object sender, MenuClickedEventArgs e)
        {
            
        }

        /// <summary>
        /// 当接收到用户输入的文本时触发此事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnTextReceived(object sender, TextReceivedEventArgs e)
        {
            if (string.IsNullOrEmpty(e.Content))
            {
                return;
            }
            if ("0,h,m,菜单,关注,home,首页".IndexOf(e.Content.ToLower(), StringComparison.Ordinal) != -1)
            {
                try
                {
                    var news = new NewsMsg(e.ToUserName, e.FromUserName)
                    {
                        Articles = Welcome.Articles
                    };
                    var xml = news.ToString();
                    Logger.Log(string.Format("\r\nResponse:\r\n{0}", xml));
                    WeChatResponse.Write(xml);
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                }
            }
            else
            {
                try
                {
                    var news = new TextMsg(e.ToUserName, e.FromUserName)
                    {
                        Content = "您好，输入0可以回到首页。"
                    };
                    var xml = news.ToString();
                    Logger.Log(string.Format("\r\nResponse:\r\n{0}", xml));
                    WeChatResponse.Write(xml);
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                }
            }
        }

        /// <summary>
        /// 当接收到用户发送的图片时触发此事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnImageReceived(object sender, ImageReceivedEventArgs e)
        {
            
        }
        /// <summary>
        /// 当接收到用户发送的链接时触发此事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnLinkReceived(object sender, LinkReceivedEventArgs e)
        {
            
        }
        /// <summary>
        /// 当接收到用户发送的地理位置信息时触发此事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnLocationReceived(object sender, LocationReceivedEventArgs e)
        {
            
        }
        /// <summary>
        /// 当接收到用户发送的语音信息时触发此事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnVoiceReceived(object sender, VoiceReceivedEventArgs e)
        {
            
        }
        /// <summary>
        /// 当接收到用户发送的视频信息时触发此事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnVideoReceived(object sender, VideoReceivedEventArgs e)
        {
            
        }
    }
}